// utils/validate.js
export const validate = {
  // 手机号验证
  phone(phone) {
    const reg = /^1[3-9]\d{9}$/;
    return reg.test(phone);
  },

  // 邮箱验证
  email(email) {
    const reg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    return reg.test(email);
  },

  // 身份证验证
  idCard(idCard) {
    const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    return reg.test(idCard);
  },

  // 必填字段验证
  required(value) {
    return value !== null && value !== undefined && value.toString().trim() !== '';
  },

  // 长度验证
  length(value, min, max) {
    const len = value ? value.toString().length : 0;
    return len >= min && len <= max;
  },

  // 数字范围验证
  numberRange(value, min, max) {
    const num = Number(value);
    return !isNaN(num) && num >= min && num <= max;
  }
};

// 验证规则配置
export const validationRules = {
  name: [
    { validator: validate.required, message: '姓名不能为空' },
    { validator: (v) => validate.length(v, 2, 20), message: '姓名长度为2-20个字符' }
  ],
  phone: [
    { validator: validate.required, message: '手机号不能为空' },
    { validator: validate.phone, message: '请输入正确的手机号码' }
  ],
  email: [
    { validator: validate.email, message: '请输入正确的邮箱地址' }
  ],
  birthday: [
    { validator: validate.required, message: '请选择生日' }
  ]
};